﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SmallBusinessKit.DataAccess.Repositories;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.Tests.Repositories
{
    /// <summary>
    /// Unit Tests for XmlNewsRepository Logic
    /// </summary>
    [TestClass]
    public class XmlNewsRepositoryTests : UnitTestBase
    {
        #region Properties

        private XmlNewsRepository _xmlRep;
        
        #endregion Properties

        #region Tests

        [TestInitialize]
        public override void Setup()
        {
            base.Setup();

            var data = XmlData.ParseNews(); // to use data from file to save time...

            var news = new List<News>();    // or create data to eliminate external dependancies (do whatever works for you, I'm not evangelical about it)
            for (var i = 0; i < 4; i++)
            {
                news.Add(
                            new News
                            {
                                Id           = (i + 1).ToString().PadLeft(2,'0'),
                                Title        = "Consectetur Fabrikam Dolor Sit Amet",
                                Date         = DateTime.Parse("2006-05-30T09:48:32.0Z"),
                                Content      = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
                                ImageUrl     = "news.jpg",
                                ImageAltText = "Fabrikam - anim id est laborum",
                                IsVisible    = (i != 3) // make one element "IsVisible = false)
                            }
                    );
            }

            _xmlRep = new XmlNewsRepository(news);
        }

        [TestMethod]
        public void XmlNewsGetById()
        {
            // Act
            var result = _xmlRep.GetById("02");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(News));
            Assert.AreEqual("02", result.Id);
        }

        [TestMethod]
        public void XmlNewsGet()
        {
            // Act
            var result = _xmlRep.Get(i => i.Id == "02");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(News));
            Assert.AreEqual("02", result.Id);
        }

        [TestMethod]
        public void XmlNewsGetMany()
        {
            // Act
            var results = _xmlRep.GetMany(i => i.IsVisible).ToList();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof (IEnumerable<News>));
            Assert.AreEqual(3, results.Count());
        }

        [TestMethod]
        public void XmlNewsGetAll()
        {
            // Act
            var results = _xmlRep.GetAll().ToList();

            // Assert
            Assert.IsNotNull(results);
            Assert.AreEqual(4, results.Count());
            Assert.IsInstanceOfType(results, typeof(IEnumerable<News>));
        }

        #endregion
    }
}
